﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Fashion.Models;

namespace Fashion.ViewModels
{
    public class SearchViewModel
    {
        public List<string> Categories { get; set; }
        public List<Product> Products { get; set; }
        public int totalProducts { get; set; }

        private FashionEntities1 FashionDB = new FashionEntities1();

        public int GetTotalProducts(string sex)
        {
            var productModel = from product in FashionDB.Products
                               where product.Sex == sex
                               orderby product.DateAdded descending
                               select product;

            var products = new List<Product>(productModel);

            int totalProducts = products.Count();

            return totalProducts;
        }

        public List<Product> SearchForProducts(string keyword, int start, int offset)
        {
            var productModel = from product in FashionDB.Products
                               where product.ProductName.Contains(keyword)
                               orderby product.DateAdded descending
                               select product;
            try
            {
                this.totalProducts = productModel.Count();
                foreach (Product p in productModel)
                {
                    if (p.Sex.CompareTo("Female") == 0)
                        p.control = "Women";
                    else
                        p.control = "Men";
                }
            }
            catch (Exception e)
            {
                this.totalProducts = 0;
            }

            var productList = productModel.Skip(start).Take(offset);

            var products = new List<Product>(productList);

            return products;
        }

        public int GetBrandID(string brandName)
        {
            var brand = FashionDB.Brands.Single(b => b.BrandName == brandName);
            return brand.BrandID;
        }

        public int GetCategoryID(string cate)
        {
            var category = FashionDB.Categories.Single(c => c.Name == cate);
            return category.CategoryID;
        }

        public List<Product> SearchByCriteria(SearchCriteria criteria, int start, int offset)
        {
            // Search by OR clause
            var productModel = from product in FashionDB.Products
                               select product;

            if (!string.IsNullOrEmpty(criteria.brandName))
            {
                var temp = productModel;
                int brandID = GetBrandID(criteria.brandName);
                productModel = productModel.Where(p => p.BrandID == brandID);
                if (productModel.Count() == 0)
                    productModel = temp;
            }

            if (!string.IsNullOrEmpty(criteria.productName))
            {
                var temp = productModel;
                productModel = productModel.Where(p => p.ProductName.Contains(criteria.productName));
                if (productModel.Count() == 0)
                    productModel = temp;
            }

            if (!string.IsNullOrEmpty(criteria.category))
            {
                var temp = productModel;
                int categoryID = GetCategoryID(criteria.category);
                productModel = productModel.Where(p => p.CategoryID == categoryID);
                if (productModel.Count() == 0)
                    productModel = temp;
            }

            try
            {
                productModel = productModel.OrderByDescending(p => p.DateAdded); // for paging

                this.totalProducts = productModel.Count();
                foreach (Product p in productModel)
                {
                    if (p.Sex.CompareTo("Female") == 0)
                        p.control = "Women";
                    else
                        p.control = "Men";
                }
            }
            catch (Exception e)
            {
                this.totalProducts = 0;
            }

            var productList = productModel.Skip(start).Take(offset);

            var products = new List<Product>(productList);

            return products;
        }
    }
}